// pages/bookrack/bookrack.js
const db=wx.cloud.database()
Page({
  data: {
    collect_num:0,
    read_num:0,
    select:false,
    selected:true,
    show:false,
    book:[]
  },


  onLoad: function (options) {
    var that=this;
    that.setData({
      resdata:that.data.resdata,
    })
    var that=this
    var openid = wx.getStorageSync('openid')
    console.log(openid,2222)
    db.collection('bookrack').where({
      _openid:openid,
    })
    .get({
        success: function(res) {
        console.log(res)
         if(res.data){
          that.setData({
            resdata:res.data,
            collect_num:res.data.length
          })
        }
      }
      
    })
  },
  //  tab切换逻辑
  // select: function( e ) {
  //   var that = this;
  //   if( 'Book' == e.currentTarget.dataset.a ) {
  //     that.setData( {
  //       select:false,
  //       selected:true
  //   })
  //   } else if('Read' == e.currentTarget.dataset.b ) {
  //       that.setData( {
  //         select:true,
  //         selected:false
  //       })
  //   }
  // },

// 挑选书籍
findBook:function(e){
  wx.switchTab({
    url: '../good/good',
  })
},
//书籍管理
admin:function(){
  var that=this
  console.log(that.data.show)
  that.setData({
    show:!that.data.show
  })
},
// 删除书籍
delBook:function(options){
  var that=this
  db.collection('bookrack').doc(that.data.resdata[options.currentTarget.dataset.index]._id).remove({
    success: function(res) {
      console.log(res)
      that.onLoad()
      wx.showToast({
        title: '删除成功',
        duration: 1000
      })
    }
  })
},
jumpDetail:function(options){
  var that=this
  that.setData({
    resdata:that.data.resdata,
    // fontNum:parseInt(res.data.data.allWords/10000)
  })
  wx.navigateTo({
    url: '../detail/detail?sourceId='+that.data.resdata[options.currentTarget.dataset.index].book_sourceId,
    // data:{
    //   sourceId:that.data.resdata[options.currentTarget.dataset.index].book_ChapterID
    // }
  })
  console.log(options.currentTarget.dataset.index)
  console.log(that.data.resdata[options.currentTarget.dataset.index].book_ChapterID,"this阅读")
  console.log(options,"options阅读")
  
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})